﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;

using Joyintech.Common;
using Joyintech.BLL.inpiao365;
using Joyintech.Model.inpiao365;
using System.Data;
using System.Collections;
using System.IO;
using System.Text;

namespace Joyintech.Admin.inpiao365.general.templateincluding
{
    public partial class edit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                //判断是否为修改操作
                if (DNTRequest.GetQueryString("includeTemplate_Id") != "")
                {
                    DataBindForm();
                }
            }
        }
        
        #region 绑定表单数据

        /// <summary>
        /// 绑定表单数据
        /// </summary>
        private void DataBindForm()
        {
            DataSet ds = new DataSet();

            ds = new systemTemplateInclude_BLL().GetList("includeTemplateId=" + DNTRequest.GetQueryString("includeTemplate_Id"));

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                Joyintech.WebControls.HFPanel.BindForm(this.HFPanel1, ds.Tables[0].DefaultView);

                //判断模板文件是否存在，如果存在，则读出来，显示出来
                string templatePath = Server.MapPath("~/templates/include/" + ds.Tables[0].Rows[0]["IncludeTemplateName"].ToString());

                if (Utils.FileExists(templatePath))
                {
                    StreamReader objSR = new StreamReader(templatePath, true);
                    string strHtml = objSR.ReadToEnd();
                    this.txtTemplateContent.Text = strHtml;
                    objSR.Close();
                }
            }
        }

        #endregion

        #region 保存表单数据

        /// <summary>
        /// 保存表单数据到表systemTemplateInclude
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            systemTemplateInclude model = new systemTemplateInclude();

            string strError = string.Empty;

            strError = Joyintech.WebControls.HFPanel.FormCheck(this.HFPanel1, model);

            if (!strError.Equals(""))
            {
                MessageBox.Show(this, strError);
                return;
            }

            Joyintech.WebControls.HFPanel.GetForm(this.HFPanel1, model);

            //保存到指定的模板文件中
            string fileName = Server.MapPath("~/templates/include/" + model.IncludeTemplateName);

            Encoding encode;

            //实例化utf-8字符集编码要领
            encode = Encoding.GetEncoding(model.TemplateCharset);

            StreamWriter objSw = new StreamWriter(fileName, false, encode);

            objSw.WriteLine(this.txtTemplateContent.Text.Trim());
            objSw.Flush();
            objSw.Close();

            //操作结果
            int dataResult = 0;

            if (DNTRequest.GetQueryString("includeTemplate_Id") != "")
            {
                model.IncludeTemplateId = Convert.ToInt32(DNTRequest.GetQueryString("includeTemplate_Id").ToString());

                dataResult = new systemTemplateInclude_BLL().Update(model);
            }
            else
            {
                dataResult = new systemTemplateInclude_BLL().Add(model);
            }

            if (dataResult >= 1)
            {
                MessageBox.ShowMsgScript(this, "保存成功！", "DG.cancel();ok();");
            }
            else
            {
                MessageBox.Show(this, "保存时发生异常，请重试！");
            }
        }

        #endregion
    }
}